package com.example.myapplication.fragments;

import android.icu.number.NumberRangeFormatter;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.R;
import com.example.myapplication.bean.ClassMate;
import com.example.myapplication.utli.UtilsEnum;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.awt.font.NumericShaper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment5#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment5 extends Fragment  {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Fragment5() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment5.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment5 newInstance(String param1, String param2) {
        Fragment5 fragment = new Fragment5();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_5, container, false);
        RequestQueue queue = Volley.newRequestQueue(getContext());
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,UtilsEnum.URL.url + "classmate/detail/" + UtilsEnum.ID, new JSONObject(), (res -> {
            String s = res.toString();
            Log.e("", "onCreate: " + s);

            Gson gson = new Gson();
            ClassMate classMate = gson.fromJson(s, ClassMate.class);
            ImageView imgName = view.findViewById(R.id.img_name_my);
            ImageView imgDept = view.findViewById(R.id.img_dept_my);
            ImageView imgMajor = view.findViewById(R.id.img_major_my);
            ImageView imgPhone = view.findViewById(R.id.img_phone_my);
            TextView tvName =  view.findViewById(R.id.tv_name_my);
            TextView tvDept =  view.findViewById(R.id.tv_dept_my);
            TextView tvMajor =  view.findViewById(R.id.tv_major_my);
            TextView tvPhone =  view.findViewById(R.id.tv_phone_my);
            ClassMate.DataDTO data = classMate.getData();
            tvName.setText(data.getName());
            tvDept.setText(data.getDepartment());
            tvMajor.setText(data.getMajor());
            tvPhone.setText(data.getTelephone());
            imgName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commit(UtilsEnum.TYPE_NAME.type);
                }
            });
            imgDept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commit(UtilsEnum.TYPE_DEPT.type);

                }
            });
            imgMajor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commit(UtilsEnum.TYPE_MAJOR.type);

                }
            });
            imgPhone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commit(UtilsEnum.TYPE_PHONE.type);
                }
            });
        }),(error ->{}));
        queue.add(request);

        return view;
    }

    public void commit(int type) {
        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        updFragment updFragment = new updFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", UtilsEnum.ID);
        bundle.putInt("type",type);
        fragmentTransaction.addToBackStack(null);
        updFragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.fl_container, updFragment);
        fragmentTransaction.commit();
    }


}